package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostChangePassword;
import com.lc.pianostudent.conn.PostGetCode;
import com.lc.pianostudent.conn.SendResetPassword;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilMatches;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.AppGetVerification;
import com.zcx.helper.view.AppPassword;
import com.zcx.helper.view.AppVerification;

/**
 * Created by Administrator on 2018/6/25.
 */

public class ChangePassActivity extends BaseActivity implements View.OnClickListener{
    @BoundView(value = R.id.change_login_pass_get_code,isClick = true) private AppGetVerification change_login_pass_get_code;  //获取验证码

    @BoundView(R.id.change_login_pass_username) private TextView change_login_pass_username;
    @BoundView(R.id.change_pass_verification) private AppVerification change_pass_verification;
    @BoundView(R.id.new_pass) private AppPassword new_pass;
    @BoundView(R.id.confirm_new_pass) private AppPassword confirm_new_pass;
    @BoundView(value = R.id.change_pass_button,isClick = true) private LinearLayout change_pass_button;
    private SendResetPassword.SendResetPasswordInfo info;
    //发送验证码接口
    public SendResetPassword sendResetPassword = new SendResetPassword(new AsyCallBack<SendResetPassword.SendResetPasswordInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, SendResetPassword.SendResetPasswordInfo sendResetPasswordInfo) throws Exception {

            countTime.start();
            info = sendResetPasswordInfo;
            UtilToast.show(toast);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    //修改密码接口
    public PostChangePassword postChangePassword = new PostChangePassword(new AsyCallBack<PostChangePassword.PostChangePasswordInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostChangePassword.PostChangePasswordInfo postChangePasswordInfo) throws Exception {
            BaseApplication.BasePreferences.setIsLogin(false);
            finish();
            BaseApplication.INSTANCE.finishActivity(SettingActivity.class);
            if (NavigationActivity.onClickProduct != null) {
                NavigationActivity.onClickProduct.setOnClick();
            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pass);

        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("修改密码");
        change_login_pass_username.setText(BaseApplication.BasePreferences.readPhone());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.change_pass_button:
                String phone = change_login_pass_username.getText().toString().trim();
                String code = change_pass_verification.getText().toString().trim();
                String new_pass_text = new_pass.getText().toString().trim();
                String confirm_pass = confirm_new_pass.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    UtilToast.show("请输入验证码");
                }else if (TextUtils.isEmpty(new_pass_text)) {
                    UtilToast.show("请输入密码");
                }  else if (TextUtils.isEmpty(confirm_pass)) {
                    UtilToast.show("请输入确认密码");
                } else if (!new_pass_text.equals(confirm_pass)) {
                    UtilToast.show("两次输入的密码不一致");
                }else if(!Validator.isPassword2(new_pass_text)){
                    UtilToast.show("请输入6-12位英文或数字");
                }else if(!Validator.isPassword2(confirm_pass)){
                    UtilToast.show("请输入6-12位英文或数字");
                }else {

                    postChangePassword.sms_code = code;
                    postChangePassword.confirm_new_password = confirm_pass;
                    postChangePassword.new_password = new_pass_text;
                    postChangePassword.user_id = BaseApplication.BasePreferences.readUID();
                    postChangePassword.execute();
                }
                break;
            case R.id.change_login_pass_get_code:

                if (TextUtils.isEmpty(change_login_pass_username.getText().toString().trim())) {
                    UtilToast.show("请输入手机号");
                } else if (!UtilMatches.checkMobile(change_login_pass_username.getText().toString().trim())) {
                    UtilToast.show("请输入正确手机号");
                } else {
                    sendResetPassword.username = change_login_pass_username.getText().toString().trim();
                    sendResetPassword.execute();
                }

                break;

        }
    }

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(change_login_pass_get_code, 24);
                change_login_pass_get_code.setText(millisUntilFinished / 1000 + "s后重新获取");
                change_login_pass_get_code.setEnabled(false);
            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                ScaleScreenHelperFactory.getInstance().loadViewSize(change_login_pass_get_code, 24);
                change_login_pass_get_code.setText("重新获取验证码");
                change_login_pass_get_code.setEnabled(true);
            }
        }
    };
}
